package com.have.blog.xo.service.admin.impl;

import com.have.blog.base.constant.BlogConstant;
import com.have.blog.model.mapper.TWebVisitMapper;
import com.have.blog.model.models.TWebVisitExample;
import com.have.blog.utils.DateUtils;
import com.have.blog.utils.ResultUtils;
import com.have.blog.xo.constant.SysConstant;
import com.have.blog.xo.dao.admin.AdminWebVisitDao;
import com.have.blog.xo.service.admin.AdminWebVisitService;
import com.have.blog.xo.service.cache.AdminWebVisitCacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;

@Service
@SuppressWarnings("all")
public class AdminWebVisitServiceImpl implements AdminWebVisitService {
    @Autowired
    private TWebVisitMapper webVisitMapper;
    @Autowired
    private AdminWebVisitDao adminWebVisitDao;
    @Autowired
    private AdminWebVisitCacheService webVisitCacheService;

    @Override
    public Long getWebVisitCount() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime = DateUtils.getToDayStartTime();
        String endTime = DateUtils.getToDayEndTime();
        TWebVisitExample example = new TWebVisitExample();
        TWebVisitExample.Criteria criteria = example.createCriteria();
        try {
            criteria.andCreateTimeBetween(sdf.parse(startTime), sdf.parse(endTime));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return webVisitMapper.countByExample(example);
    }

    @Override
    public String getVisitByWeek() {
        Map<String, Object> resultMap = webVisitCacheService.getVisitWeek();
        if (!CollectionUtils.isEmpty(resultMap)) {
            return ResultUtils.result(SysConstant.SUCCESS, resultMap);
        }
        String toDayEndTime = DateUtils.getToDayEndTime();
        Date sevenDayBefore = DateUtils.getDate(toDayEndTime, -6);
        String sevenDayBegin = DateUtils.getOneDayStartTime(sevenDayBefore);
        List<Map<String, Object>> pvByWeek = adminWebVisitDao.getPVByWeek(sevenDayBegin, toDayEndTime);
        List<Map<String, Object>> uvByWeek = adminWebVisitDao.getUVByWeek(sevenDayBegin, toDayEndTime);

        HashMap<String, Object> pvMap = new HashMap<>();
        HashMap<String, Object> uvMap = new HashMap<>();


        for (Map<String, Object> item : pvByWeek) {
            pvMap.put(item.get("DATE").toString(), item.get("COUNT"));
        }
        for (Map<String, Object> item : uvByWeek) {
            uvMap.put(item.get("DATE").toString(), item.get("COUNT"));
        }

        ArrayList<String> sevenDayList = DateUtils.getDaysByN(7, "yyyy-MM-dd");
        ArrayList<Integer> pvList = new ArrayList<>();
        ArrayList<Integer> uvList = new ArrayList<>();

        for (String day : sevenDayList) {
            if (pvMap.get(day) != null) {
                Number pvNumber = (Number) pvMap.get(day);
                Number uvNumber = (Number) uvMap.get(day);
                pvList.add(pvNumber.intValue());
                uvList.add(uvNumber.intValue());
            } else {
                pvList.add(0);
                uvList.add(0);
            }
        }
        resultMap = new HashMap<>(BlogConstant.NUM_THREE);
        ArrayList<String> resultSevenDayList = DateUtils.getDaysByN(7, "MM-dd");
        resultMap.put("date", resultSevenDayList);
        resultMap.put("pv", pvList);
        resultMap.put("uv", uvList);

        webVisitCacheService.saveVisitWeek(resultMap, 10, TimeUnit.MINUTES);
        return ResultUtils.result(SysConstant.SUCCESS, resultMap);
    }
}
